﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HslCommunication;
using HslCommunication.Profinet.Keyence;
using HslCommunication.Profinet.Omron;

namespace NDK.AcqDevice
{
    public class OmronPLC
    {
        private OmronCipNet omronCipNet = null;
        public bool ConnectStatus = false;
        public OmronPLC()
        {

        }

        public bool Connect(string IP, int prot)
        {
            omronCipNet = new OmronCipNet(IP, prot);
            OperateResult operateResult = omronCipNet.ConnectServer();
            ConnectStatus = operateResult.IsSuccess;
            return operateResult.IsSuccess;
        }


        public bool DisConnect(string IP, int prot)
        {
            OperateResult operateResult = omronCipNet.ConnectClose();
            ConnectStatus = false;
            return operateResult.IsSuccess;
        }

        public bool Write(string address, short[] value)
        {
            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }

        public bool WriteDouble(string address, double value)
        {
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                int re = Convert.ToInt32(value * 1000);
                OperateResult operateResult = omronCipNet.Write(address, re);
                return operateResult.IsSuccess;
            }
            return false;
        }

        public bool WriteDouble(string address, double[] values)
        {
            if (ConnectStatus)
            {
                int[] re = new int[values.Length];
                for (int i = 0; i < re.Length; i++)
                {
                    re[i] = Convert.ToInt32(values[i] * 1000);
                }
                OperateResult operateResult = omronCipNet.Write(address, re);
                return operateResult.IsSuccess;
            }
            return false;
        }

        public bool WriteBool(string address, bool value)
        {
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }

        public bool ReadBool(string address)
        {
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                OperateResult<bool> operateResult = omronCipNet.ReadBool(address);
                return operateResult.Content;
            }
            return false;
        }

        public string ReadString(string address, ushort length = 20)
        {
            string String = "";
            if (address == null || address == "XXX")
            {
                return String;
            }

            if (ConnectStatus)
            {
                OperateResult<string> operateResult = omronCipNet.ReadString(address, length, Encoding.ASCII);
                return operateResult.Content;
            }
            else
            {
                return String;
            }
        }

        public List<bool> ReadBoolArray(string address, ushort length)
        {
            if (ConnectStatus)
            {
                var operateResult = omronCipNet.ReadBool(address, length);
                return operateResult.Content.ToList();
            }
            return null;
        }


        public string ReadBit(string address)
        {
            string res = "False";

            if (address == null || address == "XXX")
            {
                return "False";
            }


            if (ConnectStatus)
            {
                if (address.Contains("."))
                {
                    string zfAddr = address.Split('.')[0];
                    OperateResult<short> operateResult = omronCipNet.ReadInt16(zfAddr);

                    string str = Convert.ToString(operateResult.Content, 2);


                    if (str.Length == 16)
                    {
                        string str2 = str.Substring(1, 1);
                        if (int.Parse(str2) == 1)
                        {
                            res = "True";
                        }
                    }
                    else if (str.Length == 15)
                    {
                        res = "True";
                    }
                }
            }
            return res;
        }
        public int ReadInt32(string address)
        {
            if (ConnectStatus)
            {
                OperateResult<int> operateResult = omronCipNet.ReadInt32(address);
                return operateResult.Content;
            }
            return 0;
        }
        public int ReadInt16(string address)
        {
            if (ConnectStatus)
            {
                OperateResult<ushort> operateResult = omronCipNet.ReadUInt16(address);
                return operateResult.Content;
            }
            return 0;
        }

        public bool WriteInt16(string address, Int16 value)
        {
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }
    }
}
